﻿@{
}

<!-- 引入 layui.css -->
<link href="~/css/site.css" rel="stylesheet" />
<link href="~/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form layui-form-pane" action="">
    <div style="padding: 20px;">
        <h3><i class="layui-icon layui-icon-edit"></i> 编辑物品</h3>
    </div>
    <input type="hidden" name="id" id="id" />

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">物品编号</label>
            <div class="layui-input-inline">
                <input type="text" name="goodsCode" id="goodsCode" autocomplete="off" class="layui-input" lay-verify="required" placeholder="请输入物品编号">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">物品名称</label>
            <div class="layui-input-inline">
                <input type="text" name="goodsName" id="goodsName" autocomplete="off" class="layui-input" lay-verify="required" placeholder="请输入物品名称">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">规格型号</label>
            <div class="layui-input-inline">
                <input type="text" name="goodsModel" id="goodsModel" autocomplete="off" class="layui-input" placeholder="请输入规格型号">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">单位</label>
            <div class="layui-input-inline">
                <select name="unit" lay-filter="responsible">
                <option value="">请选择单位</option>
                <option value="个">个</option>
                <option value="包">包</option>
                <option value="件">件</option>
                <option value="千克">千克</option>
                <option value="张">张</option>
                <option value="块">块</option>
            </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">物品类型</label>
            <div class="layui-input-inline">
                  <select name="goodsType" lay-filter="responsible">
                <option value="">请选择类型</option>
                 <option value="化工类">化工类</option>
                <option value="合金类">合金类</option>
                <option value="食品类">食品类</option>
                  <option value="天然类">天然类</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">最小库存</label>
            <div class="layui-input-inline">
                <input type="number" step="0.01" name="minStock" id="minStock" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">最大库存</label>
            <div class="layui-input-inline">
                <input type="number" step="0.01" name="maxStock" id="maxStock" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">当前库存</label>
            <div class="layui-input-inline">
                <input type="number" step="0.01" name="stock" id="stock" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">单价</label>
            <div class="layui-input-inline">
                <input type="number" step="0.01" name="price" id="price" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item" pane>
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="true" title="启用">
            <input type="radio" name="status" value="false" title="禁用">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" id="remark" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <button class="layui-btn" lay-submit lay-filter="submitEdit">更新</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>

<script src="~/js/site.js"></script>
<script src="~/layui/layui.js"></script>

<script>
    layui.use(['form'], function () {
        var form = layui.form;
        var layer = layui.layer;

        // 1) 读取本地存储的id
        var id = localStorage['goodsId'];
        if (!id) {
            layer.msg('未获取到要编辑的Id', { icon: 2 });
        } else {
            // 2) 加载详情回填
            $.ajax({
                url: 'http://localhost:5141/api/Goods/DetailGoodsinfo',
                type: 'GET',
                data: { id: id },
                success: function (res) {
                    if (res && res.code === 200 && res.data) {
                        var d = res.data;
                        $('#id').val(d.id);
                        $('#goodsCode').val(d.goodsCode || '');
                        $('#goodsName').val(d.goodsName || '');
                        $('#goodsModel').val(d.goodsModel || '');
                        $('#unit').val(d.unit || '');
                        $('#goodsType').val(d.goodsType || '');
                        $('#minStock').val(d.minStock ?? '');
                        $('#maxStock').val(d.maxStock ?? '');
                        $('#stock').val(d.stock ?? '');
                        $('#price').val(d.price ?? '');
                        $('#remark').val(d.remark || '');
                        var status = d.status === true ? 'true' : 'false';
                        $("input[name='status'][value='" + status + "']").prop('checked', true);
                        form.render();
                    } else {
                        layer.msg(res && res.message ? res.message : '加载详情失败', { icon: 2 });
                    }
                },
                error: function () { layer.msg('加载详情失败', { icon: 2 }); }
            });
        }

        // 3) 提交事件 - 调用更新接口
        form.on('submit(submitEdit)', function (data) {
            var f = data.field;
            var $btn = $('button[lay-filter="submitEdit"]');
            if ($btn.prop('disabled')) return false;

            if (!f.goodsCode || !f.goodsName) {
                layer.msg('请填写物品编号和名称', { icon: 2 });
                return false;
            }

            var request = {
                id: parseInt(f.id, 10),
                goodsCode: f.goodsCode.trim(),
                goodsName: f.goodsName.trim(),
                goodsModel: (f.goodsModel || '').trim(),
                unit: (f.unit || '').trim(),
                goodsType: (f.goodsType || '').trim(),
                minStock: parseFloat(f.minStock || 0),
                maxStock: parseFloat(f.maxStock || 0),
                stock: parseFloat(f.stock || 0),
                price: parseFloat(f.price || 0),
                status: f.status === 'true',
                remark: (f.remark || '').trim()
            };

            $btn.prop('disabled', true).text('提交中...');
            $.ajax({
                url: 'http://localhost:5141/api/Goods/UpdateGoodsinfo',
                type: 'POST',
                data: JSON.stringify(request),
                contentType: 'application/json',
                success: function (res) {
                    $btn.prop('disabled', false).text('更新');
                    if (res && (res.code === 200 || res > 0)) {
                        layer.msg('更新成功', { icon: 1, time: 1200 }, function () {
                            if (window.parent && window.parent.loadPage) {
                                window.parent.loadPage('/Bom/GoodsLists', '物品管理');
                            }
                        });
                    } else {
                        layer.msg(res && res.message ? res.message : '更新失败', { icon: 2 });
                    }
                },
                error: function () {
                    $btn.prop('disabled', false).text('更新');
                    layer.msg('网络错误，请重试', { icon: 2 });
                }
            });
            return false;
        });
    });
</script>
